home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-10-05 | 5.2 KB | 253 lines | [TEXT/CWIE] |
- // © Paul B. Beeken, Work In Progress, 1994-5
- // Knowledge Software Consulting.
- //
- // These files are a mindlessly simple wrapper class for the
- // basic XCMD operations. Only one instance of the xcmdBase class is
- // generated per XCMD call but there may be many instances of the XCMDString
- // class. I have used these classes to whip out XCMD/XFCNs within hours of
- // receiving the specs. They work great for me but I will always consider
- // suggestions.
- //
- // Please, please, please, in the unlikely event you should use this stuff
- // for some commercial application I would appreciate you contacting me. If
- // its for your own use, use away. Send email: knowsoft@ios.com
- //
- // As always: this file is presented as is with no warrantees expressed or implied.
- // Swim at your own risk, etc. etc.
-
- #include <strings.h>
- #include <string.h>
- #include "xcmdStrings.h"
-
- XCmdPtr
- xcmdString::paramPtr = nil; // allocated when xcmdBase object is instantiated
-
- // sometimes we need the string explicitly.
- inline int
- xcmdString::length( void ) const
- {
- if ( isPascal ) return str.p[0];
- return strlen( str.c );
- }
-
- inline char*
- xcmdString::cString( void )
- {
- if ( isPascal ) p2cstr( str.p );
- isPascal = false;
- return str.c;
- }
-
- inline StringPtr
- xcmdString::pString( void )
- {
- if ( !isPascal ) c2pstr( str.c );
- isPascal = true;
- return str.p;
- }
-
-
- xcmdString::xcmdString( Handle s ) : isPascal( false )
- {
- str.c = new char[strlen(*s)+1];
- HLock( s );
- strcpy( str.c, *s );
- HUnlock( s );
- }
-
- xcmdString::~xcmdString()
- {
- if ( str.c != nil ) delete [] str.c;
- }
-
- xcmdString::xcmdString( const xcmdString& s ) : isPascal( false )
- { // pointer may not have been created on free store.
- str.c = new char[s.length()+1];
- strcpy( str.c, s );
- }
-
- xcmdString::xcmdString( char* s ) : isPascal( false )
- { // pointer may not have been created on free store.
- str.c = new char[strlen(s)+1];
- strcpy( str.c, s );
- }
-
- xcmdString::xcmdString( StringPtr s ) : isPascal( true )
- { // pointer may not have been created on free store.
- str.c = new char[1+s[0]];
- strncpy( str.c, (char*)s, 1+s[0] );
- }
-
- /**** String Conversions ****/
- xcmdString::xcmdString( Boolean bool ) : isPascal( true )
- {
- str.c = new char[8];
-
- BoolToStr( paramPtr, bool, str.p );
- }
-
- xcmdString::xcmdString( extended num ) : isPascal( true )
- {
- str.c = new char[12];
-
- ExtToStr( paramPtr, num, str.p );
-
- }
-
- xcmdString::xcmdString( unsigned long posNum ) : isPascal( true )
- {
- str.c = new char[12];
-
- LongToStr( paramPtr, long(posNum), str.p );
- }
-
- xcmdString::xcmdString( long num, short nd ) : isPascal( true )
- {
- str.c = new char[nd+4];
-
- NumToHex( paramPtr, num, nd, str.p );
- }
-
- xcmdString::xcmdString( long num ) : isPascal( true )
- {
- str.c = new char[12];
-
- NumToStr( paramPtr, num, str.p );
- }
-
- xcmdString::xcmdString( Point pt ) : isPascal( true )
- {
- str.c = new char[15];
-
- PointToStr( paramPtr, pt, str.p );
- }
-
- xcmdString::xcmdString( Rect& rct ) : isPascal( true )
- {
- str.c = new char[30];
-
- RectToStr( paramPtr, &rct, str.p );
- }
-
- // conversion operators for useful types:
- xcmdString::operator Boolean()
- {
- Boolean rc = StrToBool( paramPtr, pString() );
- return rc;
- }
-
- xcmdString::operator Rect()
- {
- Rect rct;
- StrToRect( paramPtr, pString(), &rct );
- return rct;
- }
-
- xcmdString::operator Point()
- {
- Point pt;
- StrToPoint( paramPtr, pString(), &pt );
- return pt;
- }
-
- xcmdString::operator extended()
- {
- extended e = StrToExt( paramPtr, pString() );
- return e;
- }
-
- xcmdString::operator unsigned long()
- {
- unsigned long d = StrToLong( paramPtr, pString() );
- return d;
- }
-
- xcmdString::operator long()
- {
- unsigned long d = StrToNum( paramPtr, pString() );
- return d;
- }
-
- xcmdString::operator StringPtr()
- {
- return pString();
- }
-
- xcmdString::operator char*()
- {
- return cString();
- }
-
-
- xcmdString::operator Handle()
- {
- Handle h;
-
- h = NewHandleClear (length() + 1);
- if ( h )
- strcpy( (char *) *h, cString() );
-
- return h;
- }
-
- // Some key testing operators. == != contains
- // ____________________________________
- int
- xcmdString::contains( xcmdString& s2 )
- {
- char* op = StringMatch( paramPtr, s2, cString() );
- if ( op == nil ) op = cString();
- return op-cString();
- }
-
- Boolean
- operator!=( const xcmdString& s1, const xcmdString& s2 )
- {
- Boolean rc = StringEqual( xcmdString::paramPtr, s1, s2 );
- return !rc;
- }
-
- // Equate. Is a friend so that "" clauses are handled automaticlly
- // ____________________________________
- Boolean
- operator==( const xcmdString& s1, const xcmdString& s2 )
- {
- Boolean rc = StringEqual( xcmdString::paramPtr, s1, s2 );
- return rc;
- }
-
- // overloading of assignment.
- // ____________________________________
- xcmdString&
- xcmdString::operator=( const xcmdString& s2 )
- {
- if ( this != &s2 ) {
- delete [] str.c;
- str.c = new char[ 1 + s2.length() ];
- strcpy( cString(), s2 );
- }
- return *this;
- }
-
- // overloading of index to return specific element.
- // ____________________________________
- char
- xcmdString::operator[]( const int i )
- {
- if ( i>length() ) return '\0';
- return cString()[i];
- }
-
- // Catenation. Is a friend so that "" are handled automaticlly
- // ____________________________________
- xcmdString
- operator&( const xcmdString& s1, const xcmdString& s2 )
- {
- char* s = new char[ s1.length() + s2.length() + 1 ];
- strcpy( s, s1 );
- xcmdString rs( strcat( s, s2 ) );
- delete [] s;
- return rs;
- }
-
-